import {CustomerPCBAAdd, CustomerPCBAData, CustomerPCBADetail, CustomerPCBAParams} from "@/pages/customerConsulting/pcba/type";
import {request} from "@@/plugin-request/request";
import download from "@/utils/download";
import {HttpResult, Page} from "@/services/typings";


export const customerPCBAPageList = async (customerPCBAParams: CustomerPCBAParams) => {
    return request<HttpResult<Page<CustomerPCBAData>>>('/api/customer/pabc/list', {
        method: 'POST',
        data: customerPCBAParams
    });
}


export const customerPCBAUpdate = async (customerPCBAAdd: CustomerPCBAAdd) => {
    return request<HttpResult<string>>('/api/customer/pabc/update', {
        method: 'POST',
        data: customerPCBAAdd
    });
}

export const customerPCBAAdd = async (customerPCBAAdd: CustomerPCBAAdd) => {
    return request<HttpResult<string>>('/api/customer/pabc/add', {
        method: 'POST',
        data: customerPCBAAdd
    });
}

export const customerPCBARemove = async ( ids: (number | string)[] ) => {
    return request<HttpResult<string>>('/api/customer/pabc/remove', {
        method: 'POST',
        data: {
            ids
        }
    });
}

export const customerPCBADetail = async (id: number) => {
    return request<HttpResult<CustomerPCBADetail>>('/api/customer/pabc/detail', {
        method: 'POST',
        data: {
            id
        }
    });
}

export const customerPCBAImport = async (formData: FormData) => {
    return request<HttpResult<string>>('/api/customer/pabc/import', {
        method: 'POST',
        data: formData,
        requestType: "form"
    });
}


const customerPCBAExport = async (ids: (number | string)[]) => {
    return request<HttpResult<string>>('/api/customer/pabc/export', {
        method: 'POST',
        data: {ids},
        responseType: 'blob',
        getResponse: true,
    });
}

export const downloadCustomerPCBAExport = async (ids: (number | string)[]) => {
    await customerPCBAExport(ids).then(({data, response}) => {
        download(data, response)
    });
}


const customerPCBAExportTemplate = async () => {
    return request<HttpResult<string>>('/api/customer/pabc/export/template', {
        method: 'POST',
        responseType: 'blob',
        getResponse: true,
    });
}

export const downloadCustomerPCBAExportTemplate = async () => {
    await customerPCBAExportTemplate().then(({data, response}) => {
        download(data, response)
    });
}




const downloadPcbFile = async (id: number) => {
  return request<HttpResult<string>>('/api/customer/pabc/downloadPcbFile', {
    method: 'POST',
    responseType: 'blob',
    getResponse: true,
    data: {
      id
    }
  });
}

export const downloadCustomerPCBAFile = async (id: number) => {
  await downloadPcbFile(id).then(({data, response}) => {
    download(data, response)
  });
}
